/**
 * @function DOM本地存储
 * @encode utf-8
 * @copyright (C) lijun
 * @file storage.js
 * @author lijun
 * @version 1.0
 * @date 2010-3-14
 */

XLib.Storage = {
    storage : null,
    user_data_id : "xlib_dom_cache_container",
    user_data_key : "xlib_dom_cache",
    isInit : false,
    type : NaN,    
    Types : {
        HTML5 : 0,
        FF : 1,
        IE : 2
    },
    /**
     * 初始化Storage
     */
    init : function()
    {
        if(false === this.isInit)
        {
            if("localStorage" in window)
            {
                this.storage = localStorage;
                this.type = this.Types.HTML5;
                if(undefined === this.storage)
                {
                    this.initIE();
                }
            }
            else if("globalStorage" in window)
            {
                this.storage = globalStorage;
                this.type = this.Types.FF;
            }
            else
            {
                this.initIE();
            }
            this.isInit = true;
        }
        
    },
    /**
     * 初始化IE的Stroage
     */
    initIE : function()
    {
        var o = XLib.DOM.$E(this.user_data_id);
        if(null == o)
        {
            o = XLib.DOM.$C("link");
            o.id = this.user_data_id;            
            XLib.DOM.$A(o, XLib.DOM.$T("head")[0]);
        }
        //添加交互
        o.addBehavior("#default#userData");
        if(o.addBehavior)
        {
            o.load(this.user_data_key);
            this.storage = o;
            this.type = this.Types.IE;
        }
        else
        {
            XLib.DOM.$D(o, XLib.DOM.$T("head")[0]);
            throw new Error("Runtime error, your browser don't support the dom cache.");
        }
    },
    /**
     * 设置指定键的值
     * @param key 键
     * @param value 值
     * @param expired 过期时间(ms)
     * @param namespace 命名空间
     */
    set : function(key, value, expired, namespace)
    {
        this.init();
        
        var d = new Date();
        var v = "";
        namespace = namespace || document.domain;
        expired = expired && expired > 0 ? d.dateAdd("i", expired).getTime() : -1;
        v = "{\"value\" : \"" + encodeURIComponent(value.toString()) + "\", \"expired\" : " + expired + "}";
        switch(this.type)
        {
            case this.Types.HTML5:
                 this.storage.setItem(key, v);
                break;
            case this.Types.FF:
                this.storage[namespace][key] = v;
                break;
            case this.Types.IE:
                this.storage.setAttribute(key, v);
                this.storage.save(this.user_data_key);
                break;
        }
    },
    /**
     * 设置指定键的值
     * @param key 键
     * @param namespace 命名空间
     */
    get : function(key, namespace)
    {
        this.init();
        
        namespace = namespace || document.domain;
        var d = new Date().getTime();
        var v = null;
        var o = null;
        try{
            switch(this.type)
            {
                case this.Types.HTML5:
                    v = this.storage.getItem(key);
                    break;
                case this.Types.FF:
                    v = this.storage[namespace][key];
                    break;
                case this.Types.IE:
                    v = this.storage.getAttribute(key);
                    break;
            }
            if(null != v)
            {
                eval("o = " + v);
                
                if(o && o.expired)
                {
                    if(-1 == o.expired)
                    {
                        v = decodeURIComponent(o.value);
                    }
                    else if(d > o.expired)
                    {
                        this.remove(key, namespace);
                        v = null;
                    }
                    else
                    {
                        v = decodeURIComponent(o.value);
                    }
                }
                else
                {
                    v = decodeURIComponent(o.value);
                }
            }
        }catch(e){
            this.remove(key, namespace);
            v = null;
        }finally{
            return v;
        }
    },
    /**
     * 设置指定键的值
     * @param key 键
     * @param namespace 命名空间
     */
    remove : function(key, namespace)
    {
        this.init();
        
        namespace = namespace || document.domain;
        switch(this.type)
        {
            case this.Types.HTML5:
                this.storage.removeItem(key);
                break;
            case this.Types.FF:
                delete this.storage[namespace][key];
                break;
            case this.Types.IE:
                this.storage.removeAttribute(key);
                break;
        }
    }
};